<template>
  <transition name="fade">
    <div
      class="drawer-box"
      v-show="visible"
    >
      <transition name="fade">
        <div class="mask" @click="clickMask" v-show="mask"></div>
      </transition>
      <transition :name="'move-' + position">
        <div
          class="drawer-content"
          v-show="visible"
          :style="mainStyles"
        >
          <div class="close-btn" v-if="closable">
            <i class="el-icon-close close" @click="clickClose"></i>
          </div>
          <slot name="header">
            <header class="drawer-header" >
              <div class="drawer-header-txt">
                {{ header }}
              </div>
            </header>
          </slot>
          <slot></slot>
        </div>
      </transition>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'Drawer',
  data() {
    return {
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    header: {
      type: String,
      default: ""
    },
    position: {
      validator (value) {
        return value == 'left' ? 'left' : 'right'
      },
      default: 'right'
    },
    mask: {
      type: Boolean,
      default: true
    },
    // 如果closable为true  则显示关闭按钮
    closable: {
      type: Boolean,
      default: false
    },
    maskClosable: {
      type: Boolean,
      default: true
    },
    width: {
      type: [String, Number],
      default: "200px"
    },
  },
  computed: {
    mainStyles () {
      let style = {}
      const width = parseInt(this.width)
      const styleWidth = {
          width: width <= 100 ? `${width}%` : `${width}px`
      }

      Object.assign(style, styleWidth)
      if(this.position == 'left') {
        Object.assign(style, {
          left: 0,
        })
      } else {
        Object.assign(style, {
          right: 0,
        })
      }
      return style;
    },
  },
  mounted() {
  },
  methods: {
    clickMask() {
      // maskClosable 为false  点击遮罩层不能关闭抽屉组件
      if(this.maskClosable) {
        this.$emit('update:visible', false)
      }
      // 组件关闭回调
      this.$emit('on-close')
    },
    clickClose() {
      this.$emit('update:visible', false)
      // 组件关闭回调
      this.$emit('on-close')
    }
  }
}
</script>

<style scoped>
.move-right-enter-active,
.move-right-leave-active {
  transition: all 0.25s ease-out;
}
.move-right-enter,
.move-right-leave-to {
  -webkit-transform: translateX(200px);
  transform: translateX(200px);
  opacity: 0;
}
.move-left-enter-active,
.move-left-leave-active {
  transition: all 0.25s ease-out;
}
.move-left-enter,
.move-left-leave-to {
  -webkit-transform: translateX(-200px);
  transform: translateX(-200px);
  opacity: 0;
}
.drawer-box,
.mask {
  position: fixed;
  right: 0;
  top: 0;
  width: 100%;
  height: 100vh;
}
.mask {
  position: absolute;
  background-color: rgba(0,0,0,0.3);
}
.drawer-content {
  position: absolute;
  top: 0;
  width: 200px;
  height: 100vh;
  background-color: #fff;
  z-index: 9;
}
.drawer-header {
  border-bottom: 1px solid #e8eaec;
  padding: 14px 16px;
  line-height: 1;
}
.drawer-header-txt {
  display: inline-block;
  width: 100%;
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  color: #17233d;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.close-btn {
  z-index: 1;
  font-size: 12px;
  position: absolute;
  right: 16px;
  top: 15px;
  overflow: hidden;
  cursor: pointer;
}
.close {
  font-size: 16px;
  color: #999;
  transition: color .2s ease;
  position: relative;
  top: 1px;
}
.close:hover {
  color: #444;
}
</style>

